package com.shb.jutils.commons;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class CmdUtil {
	/**
	 * 执行命令
	 * @param cmd	命令语句
	 */
	public static void exec(String cmd){
		try {
			Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
	
	/**
	 * 执行命令，并返回命令执行后的输出内容
	 * @param cmd
	 * @return
	 */
	public static String execRtStr(String cmd){
		StringBuffer bf = new StringBuffer();
		InputStream in = null;
		InputStreamReader reader = null;
		BufferedReader br = null;
		try {
			Process process = Runtime.getRuntime().exec(cmd);
			in = process.getInputStream();
			reader = new InputStreamReader(in, "utf-8");
			
			br = new BufferedReader(reader);
			String str = null;
			while((str = br.readLine()) != null){
				bf.append(str + "\r\n");
			}
			System.out.println(bf);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					br = null;
				}
			}
			if(reader != null){
				try {
					reader.close();
				} catch (IOException e) {
					reader = null;
				}
			}
			if(in != null){
				try {
					in.close();
				} catch (IOException e) {
					in = null;
				}
			}
		}
		return bf.toString();
	}
	
	/**
	 * 执行命令并阻塞线程直到命令执行完成
	 */
	public static boolean execTillFinish(String cmdStr){
		boolean success = true;
		Process process = null;
		try{
			process = Runtime.getRuntime().exec(cmdStr);
			final InputStream errorStream = process.getErrorStream();
			final InputStream inputStream = process.getInputStream();
			
			//启动两个线程，一个线程负责读标准输出流，另一个负责读标准错误流  
			 new Thread(){
				 @Override
				 public void run() {
					String line = null;
					InputStreamReader in = null;
					BufferedReader br = null;
					try{
						in = new InputStreamReader(errorStream);
						br = new BufferedReader(in);
						while((line = br.readLine()) != null){
							System.out.println(line);
						}
					}catch (IOException e) {
						e.printStackTrace();
					}finally{
						if(br != null){
							try{
								br.close();
							}catch (IOException e) {
								br = null;
							}
						}
						if(in != null){
							try{
								in.close();
							}catch (IOException e) {
								in = null;
							}
						}
					}
				 };
			 }.start();
			
			new Thread(){
				@Override
				public void run() {
					String line = null;
					InputStreamReader in = null;
					BufferedReader br = null;
					try{
						in = new InputStreamReader(inputStream);
						br = new BufferedReader(in);
						while((line = br.readLine()) != null){
							System.out.println(line);
						}
					}catch (IOException e) {
						e.printStackTrace();
					}finally{
						if(br != null){
							try{
								br.close();
							}catch (IOException e) {
								br = null;
							}
						}
						if(in != null){
							try{
								in.close();
							}catch (IOException e) {
								in = null;
							}
						}
					}
				};
			}.start();
			
			//按照惯例，0表示正常终止
			if(process.waitFor() != 0){
				success = false;
			}
		}catch (InterruptedException | IOException e) {
			e.printStackTrace();
			success = false;
		}finally{
			try{
				if(process != null){
					process.getErrorStream().close();
					process.getInputStream().close();
				}
			}catch (IOException e) {
			}
		}
		return success;
	}
}
